package com.xx.controller;

import com.github.pagehelper.PageInfo;
import com.xx.entity.Employees;
import com.xx.entity.R;
import com.xx.enums.HttpEnum;
import com.xx.service.IEmployeesService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/employees")
@CrossOrigin //解决跨域问题
public class EmployeesController {
    @Autowired
    IEmployeesService iEmployeesService;

    //获取所有信息
    @GetMapping
    public List<Employees> getList(String empName, String phoneNumber){
        return iEmployeesService.selectAllByEmpNameAndPhoneNumber("","");
    }

    @GetMapping("byName")
    public Employees getListByLoginNameAndPassword(String loginName, String password){
        return iEmployeesService.selectAllByLoginNameAndPassword(loginName,password);
    }

    @GetMapping("page")
    public R<PageInfo<Employees>> getPage(String empName, String phoneNumber, Integer pageNum){
        PageInfo<Employees> bookPageInfo = iEmployeesService.getPage(empName,phoneNumber,pageNum);
        return R.success(bookPageInfo);
    }

    //新增
    @PostMapping
    public R saveEmployees (@Valid Employees employees){
        int count = iEmployeesService.insertBatch(employees);
        return count > 1?R.fail(HttpEnum.ERROR.getCode(),"新增失败"):R.success("新增成功");
    }

    //修改
    @PutMapping
    public R updateEmployee(@RequestBody Employees employees){
        int count = iEmployeesService.updateSelective(employees);
        return count > 1?R.fail(HttpEnum.ERROR.getCode(),"修改失败"):R.success("修改成功");
    }

    //删除
    @DeleteMapping("/{employeeId}")
    public R delEmployee(@PathVariable Integer employeeId){
        int count = iEmployeesService.delByEmployeeId(employeeId);
        return count > 1?R.fail(HttpEnum.ERROR.getCode(),"删除失败"):R.success("删除成功");
    }

    @GetMapping("{id}")
    public Employees selectById(@PathVariable Integer employeeId){
         return iEmployeesService.selectById(employeeId);
    }
}
